import { TaskStatus } from 'admin/schema/Task';
import { Progress } from 'antd';
import React from 'react';
import './style.scss';

/**通用方法：根据value获取label显示文字 */
export function getLabelName(options: any[], val?: number | string) {
  const arr = options.find(item => item.value === val);
  return arr ? arr.label : '';
}

/**任务类型 */
export const flowTypeOption = [
  { value: -1, label: '全部' },
  { value: 0, label: '机器人任务' },
  { value: 1, label: '通知流程任务' },
  // { value: 2, label: '人机协同任务' },
  // { value: 3, label: '人工自动外呼任务' },
];

/**任务类型-name */
export const getFlowTypeName = (val: number) => getLabelName(flowTypeOption, val);

/**任务流程类型 */
export const taskFlowTypeOption = [
  { value: -1, label: '全部' },
  { value: 0, label: '机器人流程' },
  { value: 1, label: '通知流程' },
  { value: 2, label: '人机协同' },
  { value: 3, label: '人工自动外呼' },
];

/**任务流程类型-name */
export const getTaskFlowTypeName = (val: number) => getLabelName(taskFlowTypeOption, val);

/**任务执行状态 */
export const taskStatusOption = [
  { value: 0, label: '全部' },
  { value: 1, label: '待执行' },
  { value: 5, label: '禁呼中' },
  { value: 2, label: '进行中' },
  { value: 7, label: '已暂停' },
  { value: 4, label: '已过期' },
  { value: 6, label: '已取消' },
  { value: 3, label: '已完成' },
];

/**
 * 获取任务状态描述name
 * - 禁呼中:一天当中，晚上或者中午是不允许给客户打电话的，所有设置了一个时间范围，不能呼叫用户，比如说12:00 到 13点 不允许呼叫  12点到13点，就是禁呼中状态
 */
export const getStatusName = (val: TaskStatus) => getLabelName(taskStatusOption, val);

/**转人工状态(可多选，pageSelect组件) */
export const transferManualStatusOption = [
  { value: 0, label: '未转人工' },
  { value: 1, label: '成功' },
  { value: 2, label: '失败' },
];

/**转人工状态-name */
export const getTransferManualStatusName = (val: number) =>
  getLabelName(transferManualStatusOption, val);

/**挂机方 */
export const hangupDirectOption = [
  { value: -1, label: '全部' },
  { value: 1, label: '用户' },
  { value: 2, label: '坐席' },
  { value: 3, label: '平台' },
  { value: 4, label: '机器人' },
];

/**挂机方-name */
export const getHangupDirectName = (val: number) => getLabelName(hangupDirectOption, val);

/**通话类型 */
export const callTypeOption = [
  { value: -1, label: '全部' },
  { value: 1, label: '人机协同' },
  { value: 2, label: '人工介入' },
  { value: 3, label: '机器人' },
];

/**通话类型-name */
export const getCallTypeOption = (val: number) => getLabelName(callTypeOption, val);

/**放音方式 */
export const soundTypeOption = [
  { value: -1, label: '全部' },
  { value: 0, label: 'TTS放音' },
  { value: 1, label: '文件放音' },
];

/**放音方式name*/
export const getSoundTypeOption = (val: number) => getLabelName(soundTypeOption, val);

export const userIntentionOption = [
  { value: 0, label: '无意愿' },
  { value: 1, label: '一般意愿' },
  { value: 2, label: '强烈意愿' },
];
/**用户意向name*/
export const getUserIntentionOption = (val: number) => getLabelName(userIntentionOption, val);

/**
 * 获取任务完成进度条dom
 */
export function getTaskProcessDom(record: any): JSX.Element {
  const process = Math.floor(
    (record.totalTaskNum ? record.finishedTaskNum / record.totalTaskNum : 0) * 100
  );
  const format = (percent?: number, successPercent?: number) => {
    return `${record.finishedTaskNum || 0}/${record.totalTaskNum || 0}`;
  };
  const successProcess = (
    <Progress status="success" percent={process} format={format} size="small" />
  );
  const errProcess = <Progress status="exception" percent={process} format={format} size="small" />;
  const normalProcess = <Progress status="active" percent={process} format={format} size="small" />;
  const statusMap: any = {
    0: normalProcess,
    1: normalProcess,
    2: normalProcess,
    3: successProcess,
    4: normalProcess,
    5: errProcess,
    6: errProcess,
    7: errProcess,
  };
  return <div className="process-task">{statusMap[record.taskStatus]}</div>;
}

/**
 * 获取呼叫状态展示的dom结构(人工监听和chat组件用到)
 */
export const getStateDom = (status: number): JSX.Element => {
  const notCalled = <div className="task-callstatus-notcall">未呼叫</div>;
  const inCall = <div className="task-callstatus-incall task-callstatus-icon">呼叫中</div>;
  // 因外呼中心，状态变更，2，3都是推送2，暂时未用到inWait
  // const inWait = <div className="task-callstatus-inwait">等待中</div>;
  const inTalk = <div className="task-callstatus-intalk task-callstatus-icon">通话中</div>;
  let str;
  switch (status) {
    case 0:
      str = notCalled;
      break;
    case 1:
      str = inCall;
      break;
    case 2:
      str = inTalk;
      break;
    case 3:
      str = inTalk;
      break;
    default:
      str = <></>;
      break;
  }
  return str;
};

/**【呼叫结果】列表
 * 场景：任务详情通话记录、通话记录报表(可多选，pageSelect组件)
 */
export const callStatusOption = [
  { value: 1, label: '成功' },
  { value: 2, label: '关机' },
  { value: 3, label: '空号' },
  { value: 4, label: '停机' },
  { value: 5, label: '通话中' },
  { value: 6, label: '用户拒接' },
  { value: 7, label: '无法接通' },
  { value: 8, label: '暂停服务' },
  { value: 9, label: '用户正忙' },
  { value: 10, label: '拨号方式不正确' },
  { value: 11, label: '呼入限制' },
  { value: 12, label: '来电提醒' },
  { value: 13, label: '呼叫转移失败' },
  { value: 14, label: '网络忙' },
  { value: 15, label: '无人接听' },
  { value: 16, label: '欠费' },
  { value: 17, label: '无法接听' },
  { value: 18, label: '改号' },
  { value: 19, label: '线路故障' },
  { value: 20, label: '稍后再拨' },
  { value: 21, label: '呼损' },
  { value: 51, label: '黑名单用户' },
  { value: 52, label: '呼叫频次限制' },
];

/**获取呼叫结果name */
export const getCallStatusName = (val?: number): string => getLabelName(callStatusOption, val);

/**任务类型 */
export const callModeOption = [
  { value: -1, label: '全部' },
  { value: 0, label: '预览式外呼' },
  { value: 1, label: '预测式外呼' },
];

/**获取呼叫结果name */
export const getCallModeName = (val?: number): string => getLabelName(callModeOption, val);

/**执行时间周期类型 */
export const callTimeOptions = [
  { label: '每天', value: 'daily' },
  { label: '每周', value: 'weekly' },
  { label: '每月', value: 'monthly' },
];
/**每周:1,2,3,4,5,6,7 */
export const weeksData = [...Array(7)].map((item, index) => index + 1);
/**每月:1,2,3,...,31 */
export const monthsData = [...Array(31)].map((item, index) => index + 1);

/**
 * 提取执行时间段
 */
export function getAllowTimes(str: string) {
  try {
    const times = JSON.parse(str);
    let res = '';
    times.forEach((item: { start: string; end: string }) => {
      res += `${item.start}-${item.end} `;
    });
    return res;
  } catch (e) {
    return '';
  }
}
